package com.itheima.jobs;

import com.itheima.common.RedisConst;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
* @Author : wangjie
* @CreateDate : 2020/4/2 9:27
* @Version : 1.0
* @Description : 清除数据任务类
*/
public class ClearJobs {
    //依赖注入redis连接对象
    @Autowired
    private JedisPool jedisPool;
    /**
     * 清除垃圾图片
     */
    public void clearImageJobs(){
        System.out.println("clearImage...");
        //通过差值获取垃圾
        Jedis jedis = jedisPool.getResource();
        Set<String> sdiff = jedis.sdiff(RedisConst.SETMEAL_PIC_RESOURCES, RedisConst.SETMEAL_PIC_DB_RESOURCES);
        //遍历删除redis和七牛云中的数据
        for (String imageFileName : sdiff) {
            QiniuUtils.deleteFile2QiNiu(imageFileName);
            jedis.srem(RedisConst.SETMEAL_PIC_RESOURCES,imageFileName);
        }
    }
}
